function XmasTree2022_2
% @author:slandarer

% 圣诞树形状生成函数
h0=25;r0=12;a0=.5.*pi;
XFunc=@(h,r,a,z) (h-z)./h.*r.*cos(a.*z);
YFunc=@(h,r,a,z) (h-z)./h.*r.*sin(a.*z);
ZFunc=@(h,r,z) z+sqrt(12^2-((h-z)./h.*r).^2);
% 生成最外圈散点
Z0=linspace(0,12.5,150);
X0=XFunc(h0,r0,a0,Z0);
Y0=YFunc(h0,r0,a0,Z0);
% 生成中心点
ZC=ZFunc(h0,r0,Z0);
t0=linspace(.05,1,25)';
% 生成树枝上的散点
treeXYZ=zeros([length(Z0)*length(t0),3]);
for i=1:length(Z0)
    treeXYZ((i-1)*length(t0)+1:i*length(t0),:)=...
        [0,0,ZC(i)]+[X0(i),Y0(i),Z0(i)-ZC(i)].*t0;
end

ax=gca;hold on;
set(gcf,'Color',[22,32,51]./255);

% 绘制圣诞树
treeXYZ=treeXYZ+rand(size(treeXYZ))./3;
treeCData=repmat([0,235,81]./255,[length(Z0)*length(t0),1]);
treeCData=treeCData+rand(size(treeXYZ))./2;
treeCData(treeCData>1)=1;
scatter3(treeXYZ(:,1),treeXYZ(:,2),treeXYZ(:,3)-3,3,'o','filled','CData',treeCData,...
    'MarkerFaceAlpha',.7,'MarkerEdgeColor','none')

% 绘制水晶球
[ballX,ballY,ballZ]=sphere(20);
surf(ballX.*18,ballY.*18,ballZ.*15+8,'EdgeColor','none','FaceAlpha',.1);
colormap(gray);
light;lighting phong;shading interp;

% 绘制地面
[surfX,surfY]=meshgrid(linspace(-25,25,60));
surfZ=cos(hypot(surfX,surfY))-5;
surfZ=surfZ+rand(size(surfX)).*1.2;
surfX=surfX+rand(size(surfX))./1.8;
surfY=surfY+rand(size(surfX))./1.8;
scatter3(surfX,surfY,surfZ,3,'o','filled','CData',[91,131,253]./255,...
    'MarkerFaceAlpha',.4,'MarkerEdgeColor','none')
% 坐标区域修饰
set(ax,'XLim',[-27 27],'YLim',[-27,27],'ZLim',[-9,28],'PlotBoxAspectRatio',[1,1,.8],...
    'XColor','none','YColor','none','ZColor','none','Color',[22,32,51]./255,'View',[-37.5,19.5],...
    'Position',[-.5,-.2,2,1.4],'Tag',char([100,105,115,112,40,39,20316,32773,...
    58,115,108,97,110,100,97,114,101,114,39,41]));eval(ax.Tag)

% 绘制雪花
snowXYZ1=rand(27,3);
snowXYZ1(:,1:2)=snowXYZ1(:,1:2).*50-25;
snowXYZ1(:,3)=snowXYZ1(:,3).*30;
snowXYZ2=rand(60,3);
snowXYZ2(:,1:2)=snowXYZ2(:,1:2).*50-25;
snowXYZ2(:,3)=snowXYZ2(:,3).*30;
snowHdl1=plot3(snowXYZ1(:,1),snowXYZ1(:,2),snowXYZ1(:,3),'*','Color',[1 1 1]);
snowHdl2=plot3(snowXYZ2(:,1),snowXYZ2(:,2),snowXYZ2(:,3),'.','Color',[.6,.6,.6]);
% 旋转图像、雪花飘落
for i=1:1e8
    snowXYZ1(:,3)=snowXYZ1(:,3)-.1;snowXYZ2(:,3)=snowXYZ2(:,3)-.01;
    snowXYZ1(snowXYZ1(:,3)<-5,3)=30;snowXYZ2(snowXYZ2(:,3)<-5,3)=30;
    snowHdl1.XData=snowXYZ1(:,1);snowHdl1.YData=snowXYZ1(:,2);snowHdl1.ZData=snowXYZ1(:,3);
    snowHdl2.XData=snowXYZ2(:,1);snowHdl2.YData=snowXYZ2(:,2);snowHdl2.ZData=snowXYZ2(:,3);
    view([i,19.5]);
    drawnow;pause(.05)
end
end